import 'package:flutter/material.dart';
import '../res/listData.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Flutter APP", style: TextStyle(color: Colors.white)),
          backgroundColor: Colors.blue,
        ),
        body: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  List<Widget> _initGridViewData() {
    var tempList = listData.map((value) {
      return Container(
        decoration: BoxDecoration(border: Border.all(color: Colors.black)),
        child: Column(
          children: [
            Image.network(value["imageUrl"]),
            SizedBox(height: 10),
            Text(value["author"], style: TextStyle(fontSize: 18)),
          ],
        ),
      );
    });
    return tempList.toList();
  }

  @override
  Widget build(BuildContext context) {
    return GridView.count(
      // 通过他可以快速的创建横轴子元素为固定最大长度的GridView
      padding: EdgeInsets.all(10), // GridView的内边距
      crossAxisSpacing: 10, // 水平子Widget之间的间距
      mainAxisSpacing: 10, // 垂直子Widget之间的间距
      crossAxisCount: 2, // 横轴子元素的数量为2
      childAspectRatio: 1, // 子Widget的长宽比
      children: _initGridViewData(),
    );
  }
}
